package wetalk.software.bupt.com.wetalk.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import wetalk.software.bupt.com.wetalk.DB.WeTalkDB;
import wetalk.software.bupt.com.wetalk.R;
import wetalk.software.bupt.com.wetalk.adapter.SearchListResultAdapter;
import wetalk.software.bupt.com.wetalk.model.po.ChatUser;
import wetalk.software.bupt.com.wetalk.model.po.SearchMessageRecord;

public class searchActivity2 extends BaseActivity implements OnItemClickListener,OnItemLongClickListener {
    SearchListResultAdapter searchListResultAdapter;
    ListView listView;
    SearchMessageRecord searchMessageRecord;
    ChatUser targetUser;
    String targetId = "";
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        listView = (ListView)findViewById(R.id.list_view_result);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        targetUser = (ChatUser) getIntent().getSerializableExtra("user");
        String msg = getIntent().getStringExtra("msg");
        targetId = String.valueOf(targetUser.getUserID());
        searchListResultAdapter = new SearchListResultAdapter(searchActivity2.this,R.layout.item_conversation,WeTalkDB.create(searchActivity2.this).queryMessageRecordByID(targetId,msg) );
        Log.i("searchActivity2", "onCreate: ");
        listView.setAdapter(searchListResultAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        searchMessageRecord = searchListResultAdapter.getItem(position);
        ChatUser user = new ChatUser();
        user.setUserId(String.valueOf(targetUser.getUserID()));
        user.setAvatar(String.valueOf(targetUser.getAvatar()));
        user.setNick(String.valueOf(targetUser.getNick()));
        user.setUserName(String.valueOf(targetUser.getUserName()));

        Intent intent = new Intent(searchActivity2.this, ChatActivity.class);
        intent.putExtra("user",user);
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }
}
